package com.csair.platform.cit.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csair.invoice.common.base.util.date.DatetimeUtil;
import com.csair.platform.cit.service.core.CheckDataPermission;
import com.csair.platform.cit.service.entity.CitDeclareRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.csair.invoice.common.base.vo.JsonResult;
import com.csair.platform.cit.facade.DeclareApi;
import com.csair.platform.cit.facade.model.CitDeclareRecordVo;
import com.csair.platform.cit.facade.model.CitSendParam;
import com.csair.platform.cit.facade.model.CitSendResultQueryParam;
import com.csair.platform.cit.service.biz.IDeclareResultService;
import com.csair.platform.cit.service.biz.IDeclareService;
import com.csair.platform.cit.service.core.CheckAnyPermission;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 企业所得税申报接口
 * @author caoyu
 * 
 */
@Api(tags = "企业所得税申报")
@Slf4j
@RestController
public class DeclareController extends BaseController implements DeclareApi {
	
	/**
	 * 企业所得税申报服务
	 */
	@Autowired
	IDeclareService declareService;
	
	/**
	 * 企业所得税申报结果服务
	 */
	@Autowired
	IDeclareResultService declareResultService;
	
	@CheckAnyPermission(values={"qysds", "citReport"})
	@CheckDataPermission()
	@ApiOperation(value = "企业所得税-申报")
	@Override
	public JsonResult<Void> send(@RequestBody CitSendParam param) {
		log.info("[企业所得税-发送申报]，请求参数:{}", JSON.toJSONString(param));
		declareService.send(param, this.getCurrentUser());
		return JsonResult.success();
	}

	@CheckAnyPermission(values={"qysds", "reportProgress"})
	@CheckDataPermission()
	@ApiOperation(value = "企业所得税-申报进度-分页查询")
	@Override
	public JsonResult<CitDeclareRecordVo> findByPage(@RequestBody CitSendResultQueryParam param) {
		log.info("[企业所得税-获取申报进度]，请求参数:{}", JSON.toJSONString(param));
    	Page<CitDeclareRecordVo> page = declareResultService.findByPage(param);
    	return JsonResult.success(page.getContent(), page.getTotalElements());
	}

	@CheckAnyPermission(values={"qysds", "reportProgress"})
	@CheckDataPermission()
	@ApiOperation(value = "企业所得税-申报进度-删除")
	@Override
	public JsonResult<Void> deleteByListId(@RequestBody List<Long> ids) {
		declareResultService.deleteByListId(ids);
		return JsonResult.success();
	}

	@CheckDataPermission
	@Override
	public JsonResult isExistsDeclareRecord(@RequestBody CitSendParam param) {
		Map<String, Object> resultMap=new HashMap<>();
		CitDeclareRecord citDeclareRecord = declareResultService.findSuccessDeclareRecord(param);
		resultMap.put("isExists",citDeclareRecord==null?"0":"1");
		resultMap.put("declareDate",citDeclareRecord==null?"":DatetimeUtil.dateToStr(citDeclareRecord.getSendTime(),DatetimeUtil.DATE_FORMAT));
		return JsonResult.success(resultMap);
	}

	@CheckDataPermission
	@Override
	public JsonResult<CitDeclareRecordVo> getById(@PathVariable Long id) {
		log.info("请求id:{}",id);
		CitDeclareRecordVo vo = declareResultService.getById(id);
		return JsonResult.success(vo);
	}
}
